<?php

namespace App\Http\Controllers\Admin\V1;

use App\Exceptions\ApiErrCode;
use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Carbon;

class UserController extends ApiController
{
    const ROUTES = [
        'index'   => 'admin.v1.app-user.index',
        'create'   => 'admin.v1.app-user.create',
        'store'   => 'admin.v1.app-user.store',
        'update'  => 'admin.v1.app-user.update',
        'destroy' => 'admin.v1.app-user.destroy',
    ];
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $code=ApiErrCode::API_CODE_OK;
        $error="操作成功";

        $usersCollections = User::orderBy('created_at','desc')->get();
        // 条件筛选
        // 1. 性别筛选
        if($request->has('gender')&&\in_array($request->get('gender'), array('0','1','2')))
        {
            $usersCollections=$usersCollections->where('sex',$request->get('gender'));
        }

        // 起始时间
        $star_time = $request->get('start_time');
        $end_time = $request->get('end_time');
        if($star_time==null || $star_time=="")
            $star_time=Carbon::create('1970-01-01 00:00:00')
                                ->setTimezone('utc')
                                ->toDateTimeString();
        if($end_time==null || $end_time=="")
            $end_time=Carbon::now()
                            ->setTimezone('utc')
                            ->toDateTimeString();
        // 2. 时间筛选
        $usersCollections=$usersCollections->whereBetween('created_at',[$star_time, $end_time]);

        // 3.电话号码筛选
        if($request->has('phone') && $request->get('phone')!="")
        {
            $usersCollections=$usersCollections->where('phone',$request->get('phone'));
        }
        
        // 4. 分页处理
        $perPage = $request->per_page ?? \count($usersCollections);
        if((int)$perPage <=0)
            $perPage=\count($usersCollections) ?? 1;
        $page = $request->page ?? 1;
        if((int)$page <=0)
            $page=1;
        
        $paginator = $usersCollections->paginate($perPage);

        $data=[];
        foreach($paginator->items() as $item)
        {
            $data[]=$item;
        }
        
        $respone = new \stdClass();
        $respone->current_page=$paginator->currentPage();
        $respone->last_page=$paginator->lastPage();
        $respone->data=$data;
        $respone->total=$paginator->total();

        return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$respone]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    // public function checkUserWithToken(Request $request)
    // {

    // }
}
